using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;

using MetX;
using MetX.IO;
using MetX.Security;
using MetX.Data;

namespace MetX.Glove.Console
{
    class Program
    {
        [STAThread()]
        static void Main(string[] args)
        {

            if (args.Length == 0)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new GloveMain());
            }
            else
            {
                string xlgdFile = null;
                string xslFile = null;
                string outputPath = null;
                string outputFile = null;

				if(args.Length == 2)
				{
					if(args[0].EndsWith(".xlgs"))
					{
						if(File.Exists(args[0]))
						{
							GloveMain.InitialXlgsFile = args[0];
							GloveMain.InitialCommand = args[1];

							Application.EnableVisualStyles();
							Application.SetCompatibleTextRenderingDefault(false);
							Application.Run(new GloveMain());
						}
					}
				}
                if (args.Length == 1)
                {
					if (args[0] == "/?" || args[0] == "-?")
					{
						System.Console.WriteLine("\tXLG.Pipeline xlgsFile []\t" + xlgdFile);
						System.Console.WriteLine("\tXLG.Pipeline xlgdFile\t" + xlgdFile);
						System.Console.WriteLine("\tXLG.Pipeline xlgdFile xslFile outputPath outputFile\t" + xlgdFile);
						System.Console.WriteLine();
						System.Console.WriteLine("\txlgsFile\tPath to a XLG Pipeline Source file to load (multi-step GUI)");
						System.Console.WriteLine("\txlgdFile\tPath to a XLG Pipeline Step Setting file (single step command line)");
						System.Console.WriteLine("\txslFile\tPath to the .xlg.xsl XSLT file used for transformation (single step command line) ");
						System.Console.WriteLine("\toutputPath\tTarget base folder for generated file(s) (single step command line)");
						System.Console.WriteLine("\toutputFile\tTarget file for generated code (single step command line)");
					}
					else if(args[0].EndsWith(".xlgs"))
					{
						if(File.Exists(args[0]))
						{
							GloveMain.InitialXlgsFile = args[0];
							Application.EnableVisualStyles();
							Application.SetCompatibleTextRenderingDefault(false);
							Application.Run(new GloveMain());
						}
					}
					else
					{
						xlgdFile = args[0];
						xslFile = xlgdFile.Replace(".xlgd", ".xlg") + ".xsl";
						outputPath = Token.BeforeLast(xlgdFile, "\\");
						outputFile = xlgdFile.Replace(".xlgd", ".Glove.cs");

						if (!File.Exists(xslFile))
						{
							System.Console.WriteLine("--- FAILED: Can't find: " + xslFile);
							return;
						}
					}
                }
                else if (args.Length == 4)
                {
                    xlgdFile = args[0];
                    xslFile = args[1];
                    outputPath = args[2];
                    outputFile = args[3];
                }

                if (xlgdFile == null || xslFile == null || outputPath == null || outputFile == null)
                {
                    System.Console.Write("--- FAILURE: Missing one or more arguments.");
                    return;
                }


				System.Console.WriteLine("\txlgdFile\t\t" + xlgdFile);
				System.Console.WriteLine("\txslFile\tt\t" + xslFile);
				System.Console.WriteLine("\toutputPath\t\t" + outputPath);
				System.Console.WriteLine("\toutputFile\t\t" + outputFile);

                try
                {
					string sxlgdFile = xlgdFile.Replace("{~}", outputPath);
					string sxslFile = xslFile.Replace("{~}", outputPath);
					CodeGenerator Gen = new CodeGenerator(sxlgdFile, sxslFile, outputPath, null);
                    FileSystem.StringToFile(outputFile, Gen.Code);
                    System.Console.WriteLine("--- SUCCESS: " + outputFile);
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine("--- FAILED: " + ex.ToString());
                }
            }
        }
    }
}
